package com.org.servlet;

import com.org.service.UserService;
import com.org.service.iml.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet" ,urlPatterns="/user")
public class LoginServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String operate = request.getParameter("operate");

       if (operate.equals("login")){
           login(request,response);
       }

        response.getWriter().flush();
        response.getWriter().close();
    }

    private void login(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
        String username = request.getParameter("userName");
        String password = request.getParameter("userPW");

        if (userService.getLoginCount(username , password) == 1){
            response.getWriter().print("true");
        }else {
            response.getWriter().print("false");
        }
    }
}
